package com.dyb.comm;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import io.minio.*;
import io.minio.messages.Item;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.InputStream;

/**
 * @ClassName : FileUploader
 * @Description :
 * @Author : LinYa
 * @Date: 2022/6/22  13:43
 */
@RestController
@ResponseBody
public class FileUploader {

    private static final String BUCKET_NAME = "default";
    @Autowired
    private MinioClient minioClient;


    @GetMapping("/add")
    public void test() throws Exception {
        ObjectWriteResponse response = minioClient.uploadObject(UploadObjectArgs.builder()
                .bucket(BUCKET_NAME)
                .object("123.jpg")
                .filename("D:\\WorkSpace\\easyexcel\\minioClient\\壁纸.jpg")
                .build());
        System.out.println(response.etag());
        System.out.println(response.versionId());
    }


    @GetMapping("/meta")
    public void test2(@RequestParam("remoteId") String remoteId) throws Exception {
        // 检查'my-bucketname'是否存在。
        boolean found = minioClient.bucketExists(BucketExistsArgs.builder().bucket(BUCKET_NAME).build());
        if (found) {
			/*==获取元数据
			 结果格式：（ObjectStat{bucket=default, object=de3cc0e5180511ed8fd702000a080367.csv, last-modified=2022-08-09T17:08:20Z, size=171}）
			 */
            StatObjectResponse statObject = minioClient.statObject(
                    StatObjectArgs.builder()
                            .bucket(BUCKET_NAME)
                            .object("de3cc0e5180511ed8fd702000a080367.csv")
                            .build());
            System.out.println(statObject.toString());

            /*================================================分割线=========================================================*/

			/*
			获取桶内所有文件
			*/
            Iterable<Result<Item>> myObjects = minioClient.listObjects(
                    ListObjectsArgs.builder()
                            .bucket(BUCKET_NAME)
                            .recursive(true)
                            .build());
            for (Result<Item> result : myObjects) {
                Item item = result.get();
                System.out.println(item.lastModified() + ", " + item.size() + ", " + item.objectName());
            }

            /*================================================分割线=========================================================*/

			/*
			下载文件
			 */
            try (InputStream stream =
                         minioClient.getObject(
                                 GetObjectArgs.builder()
                                         .bucket(BUCKET_NAME)
                                         .object(remoteId)
                                         .build())
            ) {
                FileUtil.writeFromStream(stream, "D:\\WorkSpace\\easyexcel\\minioClient\\元数据.csv");
            }
        } else {
            System.out.println("桶不存在");

        }

    }



}
